package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.ProPurchase;
import com.woniu.entity.User;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.entity.vo.PageProPurchase;
import com.woniu.entity.vo.ProPurchaseExcel;
import com.woniu.entity.vo.UserExcel;
import com.woniu.exception.controllerException.ProPurchaseExistException;
import com.woniu.service.IProPurchaseService;
import com.woniu.util.ExcelResponseHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *<p>
 *采购的前端控制
 *</p>
 * @author ZhouWu
 * @date 2022/9/2 1:10
 */

@RestController
@RequestMapping("/brain/proPurchase")
@CrossOrigin(allowCredentials = "true", originPatterns = "*")
public class ProPurchaseController {

    @Autowired
    private IProPurchaseService iProPurchaseService;

    //分页查询采购信息
    @RequestMapping("/pageList")
    public ResponseEntity getProPurchaseByPage(@RequestBody PageProPurchase pageProP ) {
        Page<ProPurchase> page = iProPurchaseService.getProPurchaseByPage(pageProP.getPageNum(),pageProP.getPageSize(),pageProP.getProPurchase() );
        return ResponseEntity.success(page.getRecords(),"分页查询成功");
    }

    //查询所有采购，不分页
    @RequestMapping("/list")
    public ResponseEntity<List<ProPurchase>> getAll() {
        ResponseEntity<List<ProPurchase>> entity = new ResponseEntity<>();
        entity.setData(iProPurchaseService.getAll());
        entity.setMsg("查询成功");
        return entity;
    }

    //根据采购id，查询采购，先写上，不一定会用
    @RequestMapping("/get/{purId}")
    public ResponseEntity getByProId(@PathVariable Integer purId ) {
        ProPurchase proPurchase = iProPurchaseService.getByPurId(purId);
        return  ResponseEntity.success(proPurchase,"查询成功");
    }

    //增加采购
    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody ProPurchase proPurchase ) {
        if(iProPurchaseService.getByPurId(proPurchase.getPurId()) != null ) {
            throw  new ProPurchaseExistException("采购已存在");
        }
        System.out.println(proPurchase.getPurPrice());
        iProPurchaseService.add(proPurchase);
        return ResponseEntity.success(null,"添加成功");
    }

    //逻辑删除采购
    @RequestMapping("/delete/{purId}")
    public ResponseEntity delete(@PathVariable Integer purId) {
        ProPurchase proPurchase = iProPurchaseService.getByPurId(purId);
        proPurchase.setPurStatus("n");  //修改采购的状态
        iProPurchaseService.update(proPurchase);
        return  ResponseEntity.success(null,"删除成功");
    }

    //根据条件修改采购
    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody ProPurchase proPurchase ) {
        iProPurchaseService.update(proPurchase);
        return ResponseEntity.success(null,"修改成功");
    }

    //下载采购表详情
    @RequestMapping("/download")
    public void downloadExcel(@RequestBody(required = false) ProPurchase proPurchase, HttpServletResponse response) throws IOException {
        System.out.println("进入");
        List<ProPurchase> proPurchaseList = iProPurchaseService.getProPurchaseByCondition(proPurchase);
        System.out.println("进入");
        List<ProPurchaseExcel> proPurchaseExcels = new CopyOnWriteArrayList<>();
        for (ProPurchase proPurchase1 : proPurchaseList) {
            ProPurchaseExcel proPurchaseExcel = new ProPurchaseExcel();
            proPurchaseExcel.setProId(proPurchase1.getProject().getProId());
            proPurchaseExcel.setPurName(proPurchase1.getPurName());
            proPurchaseExcel.setPurDetail(proPurchase1.getPurDetail());
            proPurchaseExcel.setSupplier(proPurchase1.getSupplier());
            proPurchaseExcel.setPurPrice(proPurchase1.getPurPrice());
            proPurchaseExcel.setPurNum(proPurchase1.getPurNum());
            proPurchaseExcel.setPurStatus( (proPurchase1.getPurStatus() =="y" ? "采购中":"采购结束") );
            proPurchaseExcels.add(proPurchaseExcel);
        }
        ExcelResponseHeaderUtil.setExcelRespProp(response, "采购表");
        EasyExcel.write(response.getOutputStream())
                .head(ProPurchaseExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("采购表")
                .doWrite(proPurchaseExcels);
    }


}
